package DataStructure.SegmentTree;

public class SegmentTree<E> {

    private E[] data;
    private E[] tree;
    private Merger<E> merger;

    public SegmentTree(E[] arr, Merger<E> merger) {
        data = (E[]) new Object[arr.length];
        this.merger = merger;
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        tree = (E[]) new Object[4 * arr.length];
        buildSegmentTree(0, 0, arr.length - 1);
    }

    private void buildSegmentTree(int index, int l, int r) {
        if (l == r) {
            tree[index] = data[l];
            return;
        }
        int leftTreeIndex = leftChild(index);
        int rightTreeIndex = rightChild(index);

        int mid = l + (r - l) / 2;
        buildSegmentTree(leftTreeIndex, l, mid);
        buildSegmentTree(rightTreeIndex, mid + 1, r);

        tree[index] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
    }

    public E query(int queryL, int queryR) {
        if (queryL < 0 || queryL >= data.length || queryR < 0 || queryR >= data.length || queryL > queryR) {
            throw new IllegalArgumentException("index is illegal");
        }

        return query(0, 0, data.length - 1, queryL, queryR);
    }

    private E query(int index, int l, int r, int queryL, int queryR) {
        if (l == queryL && r == queryR) {
            return tree[index];
        }

        int mid = l + (r - l) / 2;
        int leftTreeChild = leftChild(index);
        int rightTreeChild = rightChild(index);

        if (queryL > mid) {
            return query(rightTreeChild, mid + 1, r, queryL, queryR);
        } else if (queryR <= mid) {
            return query(leftTreeChild, l, mid, queryL, queryR);
        } else {
            E leftResult = query(leftTreeChild, l, mid, queryL, mid);
            E rightResult = query(rightTreeChild, mid + 1, r, mid + 1, queryR);
            return merger.merge(leftResult, rightResult);
        }
    }

    public void set(int index, E e) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("index is illegal");
        }

        set(0,0,data.length - 1, index,e);
    }

    private void set(int treeIndex, int l, int r, int index , E e) {
        if(l == r) {
            tree[treeIndex] = e;
            return;
        }

        int mid = l + (r - l) / 2;
        int rightTreeIndex =  rightChild(treeIndex);
        int leftTreeIndex = leftChild(treeIndex);

        if(index > mid) {
            set(rightTreeIndex,mid + 1,r,index,e);
        } else {
            set(leftTreeIndex,l,mid,index,e);
        }

        tree[treeIndex] = merger.merge(tree[leftTreeIndex],tree[rightTreeIndex]);
    }

    public E get(int index) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("Index is illegal");
        }
        return data[index];
    }

    public int getSize() {
        return data.length;
    }

    private int leftChild(int index) {
        return 2 * index + 1;
    }

    private int rightChild(int index) {
        return 2 * index + 2;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < tree.length; i++) {
            if (tree[i] != null) {
                stringBuilder.append(tree[i] + " ");
            } else {
                stringBuilder.append("null ");
            }
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
